package com.lizemin.jingdongmall.controller;

import com.lizemin.jingdongmall.entity.RespBean;
import com.lizemin.jingdongmall.service.IStockService;
import com.lizemin.jingdongmall.service.impl.StockServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lizemin
 * @since 2025-03-26
 */
@RestController
@RequestMapping("/stock")
public class StockController {

    @Autowired
    IStockService stockService;

    /**
     * 购买手机
     */
    @GetMapping("/buyIphone")
    public RespBean buyIphone() {
        stockService.buyIphone();
        return RespBean.success();
    }

    /**
     * 悲观锁解决超卖问题
     */
    @GetMapping("/buyIphone2")
    public RespBean buyIphone2() {
        stockService.buyIphone2();
        return RespBean.success();
    }

    /**
     * 乐观锁解决超卖问题
     */
    @GetMapping("/buyIphone3")
    public RespBean buyIphone3() {
        stockService.buyIphone3();
        return RespBean.success();
    }

    /**
     * 使用redis分布式锁解决集群环境中的超卖问题
     */
    @GetMapping("/buyIphone4")
    public RespBean buyIphone4() {
        stockService.buyIphone4();
        return RespBean.success();
    }

}
